<?php
/**
 * @author zhouguoyan
 * @desc 公司登陆,忘记密码管理,新注册Company
 **/

class CompanyController extends AppController {
	var $name = 'Company';
	
	var $uses = array(
		'Company', 'CompanyAccount', 'SysParams', 'MailJob'
	);

	/**
	 * @author zhouguoyan
	 * @desc 会员
	 **/
	
	function index() {
		$this->pageTitle = "カモメ中国転職：一般企業求人情報投稿画面";
		$start_page = '/job/index';
		$redirect = (!empty($this->params['pass'])) ? '/' . join('/', $this->params['pass']) : $start_page; //set redirect path
		if(isset($this->rdCompany->id)) {
			$this->redirect($redirect);
		}
		if(isset($_COOKIE['company_cookie'])) {
			$cookie = $_COOKIE['company_cookie'];
			$pwd = substr($cookie, 0, 32);
			$id = substr($cookie, 32);
			$data = $this->CompanyAccount->find("\"CompanyAccount\".\"id\" = '" . $id . "'" . " AND \"CompanyAccount\".\"deleted\" is null" . " AND \"CompanyAccount\".\"login_pwd\" = '" . $pwd . "'");
			if(!empty($data)) {
				$check_pwd['CompanyAccount']['company_name'] = $check_pwd['Company']['company_name'];
				$check_pwd['CompanyAccount']['company_alias'] = $check_pwd['Company']['company_alias'];
				$this->rdCompany->set($data['CompanyAccount']);
				$this->redirect("http://" . str_replace("www.", "", $_SERVER['SERVER_NAME']) . $redirect);
			}
		}
		//@author wangyuhua 2012/3/31 start
		$company_login_url = ADMIN_SERVER_NAME;
		$this->set("company_login_url",$company_login_url);
		//@author wangyuhua 2012/3/31 end
		$this->Session->write('login_redirect', $redirect);
	
	}

	/**
	 * @author zhouguoyan
	 * @desc 退出登陆
	 **/
// 	function loginout() {
		
// 		$this->rdCompany->logout();
// 		$this->delCookie("company_cookie");
// 		$this->redirect("/top/index");
// 	}

	/**
	 * @author zhouguoyan
	 * @desc 新注册 输入EMAIL
	 **/
	function reg_mail_add() {
		$this->pageTitle = "カモメ一般企業登録";
	
	}

	/**
	 * @author zhouguoyan
	 * @desc 新注册 验证EMAIL
	 **/
	function reg_mail_confirm() {
		$this->pageTitle = "カモメ一般企業登録";
		$message = $this->CompanyAccount->Email_Valid($this->Valid, $this->params['data']);
		//$hour=$this->Prop->getValue('config.register_cancel_hour');//@author wangyuhua この行を注釈する
		//$canceltime = date('Y-m-d H:i:s',mktime(date("H")-$hour));//@author wangyuhua この行を注釈する
		//@author wangyuhua 2012-02-17 update start
		if(empty($message)) {
			$email = $this->params['data']['Company']['email'];
			$re = $this->Company->find("\"Company\".\"deleted\" is null AND \"Company\".\"email\"=" . "'$email' AND \"Company\".\"company_type_id\" = 2");
			if(!empty($re)) {
				$condition = //"\"Company\".\"created\">'".$canceltime."' AND " .
				"\"Company\".\"email\"='" . $email . "' AND " . "\"Company\".\"deleted\" is null AND " . "\"Company\".\"company_name\" is not null AND " . //@author wangyuhua 2012-02-17 add
"\"Company\".\"company_type_id\" = 2"; //1:人材紹介会社　２：一般会社
				//@author wangyuhua 2012-02-17 update end
				$canceltime_validate = $this->Company->find($condition);
				if(!empty($canceltime_validate)) {
					$message .= $this->Valid->tagErrorMsg('exist', 'Company/email');
				}else {
					if(empty($re['Company']['company_name'])) {
						$re['Company']['created'] = date('Y-m-d H:i:s');
						$re['Company']['update_id'] = 5;
						$this->Company->save($re);
					}else {
						$message .= $this->Valid->tagErrorMsg('exist', 'Company/email');
					}
				}
			}
			if($message) {
				//validate failed
				$this->set("message", $message);
				$this->render('reg_mail_add');
				exit();
			}else {
				//validate success
				$this->data['Company']['update_id'] = 6;
				$this->data['Company']['company_type_id'] = 2;
				$this->Company->save($this->data);
				$url = $this->Company->find("\"Company\".\"email\"='" . $email . "' and \"Company\".\"deleted\" is null and \"Company\".\"company_type_id\" =2");
				$data['name'] = $this->params['data']['Company']['email'];
				//@author wangyuhua 2012-02-17 update start
				$timestamp = strtotime($url['Company']['created']);
				$data['url'] = "Company/reg_add/" . Md5($data['name']) . $url['Company']['id'] . $timestamp;
				//echo $data['url'] . "<br>";
				//$data['url']="Company/reg_add/".Md5($data['name']).$url['Company']['id'];//@author wangyuhua この行を注釈する
				//@author wangyuhua 2012-02-17 update end
				$contents = $this->getMailTemplate("general_regist_step1", $data);
				$subject = $this->Prop->getValue('config.mail_title.general_regist_step1');
				echo $contents;exit;
				$this->MailJob->saveMsg('web@localhost:82', $data['name'], $subject, $contents, "", 'info@localhost:82');
				$this->render('reg_mail_success');
				exit();
			}
		}else {
			
			$this->set("message", $message);
			$this->render('reg_mail_add');
			exit();
		}
	}

	/**
	 * @author zhouguoyan
	 * @desc 新注册 注册信息添加
	 **/
	function reg_add($str = null) {
		$this->setHead();
		
		$this->pageTitle = "カモメ一般企業登録";
		if(!empty($this->data)) {
			$this->render('reg_add');
			exit();
		}
		
		if($this->Session->check('reg_info')) {
			$this->data = $this->Session->read('reg_info');
			// wang.wei add 2012/5/23 start
			$this->Output->htmlspecialchars_decode($this->data);
			// wang.wei add 2012/5/23 end
			$this->set('powers', $this->SysParams->generateList($condition = "kbn='password_question_id'"));
			$year = $this->getyears();
			$month = $this->getmonth();
			$this->set('year', $year);
			$this->set('month', $month);
			$this->render('reg_add');
			exit();
		}
		
		if(!empty($str)) {
			//@author wangyuhua 2012-02-16 update start
			$id = substr($str, 32, strlen($str) - 42);
			$creTime = substr($str, strlen($id) + 32, 10);
			$conTime = date("Y-m-d H:i:s", $creTime);
			$hour = $this->Prop->getValue('config.register_cancel_hour');
			//日本時間より、一時間を加え
			$hour = $hour + 1;		
			$strsql = "select to_char((current_timestamp - interval '" . $hour . " hours'),'yyyy-mm-dd hh24:mi:ss:ms') as dbtime";
			$timeNow = $this->Company->query($strsql);
			$dbTime = $timeNow[0][0]['dbtime'];
			//$actime = date("Y-m-d H:i:s",strtotime("-1 day"));
			//$id=substr($str,32);//@author wangyuhua この行を注釈する
			$email = substr($str, 0, 32);
			$this->Session->write('reg_id', $id);
			if(!empty($id) && !empty($email)) {
				$con = "\"Company\".\"id\"=" . $id . " and \"Company\".\"company_type_id\" = '2' and cast(\"Company\".\"created\" as char(19)) like '" . $conTime . "%' and \"Company\".\"created\" > to_timestamp('" . $dbTime . " ','yyyy-mm-dd hh24:mi:ss:ms') and \"Company\".\"deleted\" is null";
				//$con="\"Company\".\"id\"=".$id." and \"Company\".\"deleted\" is null";//@author wangyuhua この行を注釈する
				$reg = $this->Company->find($con);
			}
			//@author wangyuhua 2012-02-16 update end
			if(!empty($reg)) {
				if(!empty($reg['Company']['company_name'])) {
					
					$this->showError('reg_key_date_error');
					exit();
				}
				$database_email = md5($reg['Company']['email']);
				if($email == $database_email) {
					$pwd_remain_id = $this->SysParams->read(null, $this->data['Company']['pwd_remain_id']);
					$this->data['Company']['pwd_remain_id'] = $pwd_remain_id['SysParams']['name'];
					$this->set('powers', $this->SysParams->generateList($condition = "kbn='password_question_id'"));
					$this->data['Company']['email'] = $this->data['Company']['email_confirm'] = $reg['Company']['email'];
					$this->data['Company']['id'] = $reg['Company']['id'];
					$year = $this->getyears();
					$month = $this->getmonth();
					$this->set('year', $year);
					$this->set('month', $month);
					$this->data['Company']['agree'] = 2;
				}else {
					
					$this->showError('reg_key_error');
					exit();
				}
			}else {
				$this->showError('reg_key_error');
				exit();
			}
		}else {
			$this->showError('reg_key_error');
			exit();
		}
		
		if(empty($this->data)) {
			$this->data = $this->Session->read('reg_info');
			$this->set('powers', $this->SysParams->generateList($condition = "kbn='password_question_id'"));
			$year = $this->getyears();
			$month = $this->getmonth();
			$this->set('year', $year);
			$this->set('month', $month);
			$this->render('reg_add');
			exit();
		}
	
	}

	function getyears($start = 1900) {
		$years = array();
		$year = date('Y');
		for($i = 0; $start <= $year; $i++) {
			$years[$year] = $year;
			$year--;
		}
		return $years;
	}

	function getmonth() {
		$month = array();
		for($i = 1; $i <= 12; $i++) {
			if($i < 10) {
				$month['0' . $i] = '0' . $i;
			}else {
				$month[$i] = $i;
			}
		
		}
		return $month;
	}

	/**
	 * @author zhouguoyan
	 * @desc 新注册 注册信息确认
	 **/
	function reg_confirm() {
		$this->setHead();
		
		// wang.wei add 2012/5/23 start
		$this->Output->htmlspecialchars($this->data);
		// wang.wei add 2012/5/23 end
		$this->pageTitle = "カモメ一般企業登録";
		
		$company_id = $this->data['Company']['id'];
		if (!$this->Valid->number($company_id)) {
			$this->show404();
			exit;
		}
		
		//公司创建年份
		$this->data['Company']['company_created'] = $this->params['data']['Company']['company_created_year'] . '-' . $this->params['data']['Company']['company_created_month'];
		
		$this->data['Company']['company_type_id'] = 2;
		$this->Session->write('reg_info', $this->data);
		
		//表单验证
		$message = $this->CompanyAccount->Reg_Valid($this->Valid, $this->data);
		
		//LoginId与密码验证，LoginId与密码不能相同
		if($this->data['CompanyAccount']['login_id'] == $this->data['CompanyAccount']['login_pwd'] && $this->data['CompanyAccount']['login_pwd'] != null) {
			$message .= $this->Valid->tagErrorMsg('reg_pwd_repeat', 'CompanyAccount/login_pwd');
		
		}
		if($this->Valid->required($this->params['data']['CompanyAccount']['login_id'])
				&& $this->Valid->minmaxlength($this->params['data']['CompanyAccount']['login_id'],'6-20')) {
			$condition = "\"CompanyAccount\".\"deleted\" is null AND " 
				. "\"CompanyAccount\".\"login_id\"='" 
				. $this->params['data']['CompanyAccount']['login_id'] . "'";
			$login_name = $this->CompanyAccount->find($condition);
			if($login_name) {
				$message .= $this->Valid->tagErrorMsg('exist', 'CompanyAccount/login_id');
			}
		}
		if($this->params['data']['Company']['company_alias'] == null) {
			$this->data['Company']['company_alias'] = $this->data['Company']['company_name'];
		}
		$str = $this->params['data']['Company']['incept_email'];
		//		$incept_email = $this->Company->find("Company.deleted is null AND Company.incept_email="."'$str'");	
		

		if(isset($this->params['data']['Company']['agree'])) {
			if($this->params['data']['Company']['agree'] == 2) {
				$message .= $this->Valid->tagErrorMsg('disagree', 'Company/agree');
			}
		}else {
			$message .= $this->Valid->tagErrorMsg('disagree', 'Company/agree');
		}

		if(empty($message)) {
			//email地址重复验证
			$email = addslashes($this->data['Company']['email']);
			$condition = "\"Company\".\"deleted\" is null AND " . "\"Company\".\"id\" <> $company_id AND " . "\"Company\".\"email\" = '$email' AND " . "\"Company\".\"company_type_id\" = 2"; //1：人材紹介会社　２：一般会社
			$check_mail_repeat = $this->Company->find($condition);
			if(!empty($check_mail_repeat)) {
				$message .= $this->Valid->tagErrorMsg('exist', 'Company/email');
			}
		}
		
		if($message) {
			// wang.wei add 2012/5/23 start
			$this->Output->htmlspecialchars_decode($this->data);
			// wang.wei add 2012/5/23 end
			//validate failed
			$this->set("message", $message);
			
			//公司创建年份选项初始化
			$password_question = $this->SysParams->generateList($condition = "kbn='password_question_id'");
			$year = $this->getyears();
			$month = $this->getmonth();
			$this->set('powers', $password_question);
			$this->set('year', $year);
			$this->set('month', $month);
			
			$this->render('reg_add');
			exit();
		}else {
			//validate success
			$pwd_remain_id = $this->SysParams->find("kbn='password_question_id' AND id =" . $this->data['Company']['pwd_remain_id']);
			$this->data['Company']['pwd_remain_id'] = $pwd_remain_id['SysParams']['name'];
			
			$this->render('reg_confirm');
			exit();
		}
	}

	/**
	 * @author zhouguoyan
	 * @desc 新注册 注册信息保存
	 **/
	function reg_finish() {
		$this->setHead();
		
		$this->pageTitle = "カモメ一般企業登録";
		$this->data = $this->Session->read('reg_info');
		$str = $this->data['Company']['incept_email'];
		
		$incept_email = $this->Company->find("\"Company\".\"deleted\" is null AND \"Company\".\"incept_email\"= '" . $str . "'");
		$this->data = $this->Session->read('reg_info');
		$id = $this->Session->read('reg_id');
		if(empty($id) || $id == null || empty($this->data)) {
			$this->showError('session_expired');
		}
		
		if($this->data['Company']['company_alias'] == null) {
			$this->data['Company']['company_alias'] = $this->data['Company']['company_name'];
		}
		$this->data['Company']['id'] = $id;
		$company_id = $this->data['Company']['id'];
		$email = $this->Company->find("\"Company\".\"deleted\" is null AND \"Company\".\"id\"= '" . $company_id . "'");
		$info = array();
		$info['CompanyAccount']['role'] = 'General';
		$info['CompanyAccount']['email'] = $this->data['Company']['email'];
		$info['CompanyAccount']['name'] = $this->data['CompanyAccount']['name'];
		$info['CompanyAccount']['company_id'] = $this->data['Company']['id'];
		$info['CompanyAccount']['login_id'] = $this->data['CompanyAccount']['login_id'];
		$info['CompanyAccount']['login_pwd'] = md5($this->data['CompanyAccount']['login_pwd']);
		// wang.wei add 2012/4/12 start
		$info['CompanyAccount']['last_pwd_modified'] = 'now()';
		// wang.wei add 2012/4/12 end
		if($this->Session->check('reg_info')) {
			$this->data['Company']['update_id'] = 7;
			$this->Company->save($this->data);
			$data['email'] = $email['Company']['email'];
			$data['name'] = $this->data['Company']['company_name'];
			$data['url'] = "company/index/general/detail";
			$contents = $this->getMailTemplate("general_regist_finish", $data);
			$subject = $this->Prop->getValue('config.mail_title.general_regist_finish');
			$this->MailJob->saveMsg('web@localhost:82', $data['email'], $subject, $contents, "", 'info@localhost:82');
			$this->CompanyAccount->save($info);
		}
		
		$this->render('reg_finish');
		$this->Session->del('reg_id');
		$this->Session->del('reg_info');
	}

	/**
	 * Web ページの有効期限をセットする
	 *
	 * create: 2012-04-24 ツバサ（SY）
	 * */
	private function setHead() {
		header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
		header("Last-Modified:".gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
	}	
	
	/**
	 * @author zhouguoyan
	 * @desc 找回密码 输入EMAIL
	 **/
// 	function find_pwd() {
		
// 		if(empty($this->data)) {
// 			$this->data = $this->Session->read('mail_info');
// 		}
// 	}

	/**
	 * @author zhouguoyan
	 * @desc 找回密码 输入EMAIL确认
	 **/
// 	function find_pwd_confirm() {
		
// 		$message = $this->CompanyAccount->Pwd_Email_Valid($this->Valid, $this->params['data']);
// 		$par = $this->params['data']['Company']['account_email'];
// 		$condition = "\"CompanyAccount\".\"email\"=" . "'$par'" . " and \"CompanyAccount\".\"deleted\" is null" . " and \"CompanyAccount\".\"role\" = 'General'";
// 		$email = $this->CompanyAccount->find($condition, null);
// 		if(!empty($email)) {
// 			$company_id = $email['Company']['id'];
// 			if(!empty($company_id)) {
// 				$data = $this->Company->find("\"Company\".\"id\" =" . $company_id . " and \"Company\".\"deleted\" is null");
// 			}
		
// 		}
// 		$company_id = $email['Company']['id'];
// 		if(empty($data) && !$message) {
// 			$message .= $this->Valid->tagErrorMsg('not_exist', 'Company/account_email');
// 		}
		
// 		if($message) {
// 			//validate failed
			

// 			$this->set("message", $message);
// 			$this->render('find_pwd');
// 			exit();
// 		}else {
// 			$condition1 = "\"Company\".\"deleted\" is null AND \"Company\".\"id\"=" . $company_id;
// 			$this->data = $this->Company->find($condition1);
// 			$remain_id = $this->data['Company']['pwd_remain_id'];
			
// 			//パスワード有効期限チェック　add@090825 by yangjie start	
// 			$remain_timeout = $this->Prop->getValue("config.remain_timeout_hour");
// 			$remained_key = $this->Encrypt->randKey('6');
// 			$remained = mktime(date('H') + $remain_timeout, date('i'), date('s'), date('m'), date('d'), date('Y'));
// 			$company_data['Company']['id'] = $company_id;
// 			$company_data['Company']['remained'] = date("Y-m-d H:i:s", $remained);
// 			$company_data['Company']['remain_key'] = $remained_key;
// 			$this->Company->save($company_data);
// 			//パスワード有効期限チェック　add@090825 by yangjie end
			

// 			$sever_name = FRONT_SERVER_NAME;
// 			$email_data['url'] = 'https://' . $sever_name . '/company/pwd_add/' . md5($remained_key) . $company_id;
// 			$email_data['name'] = $email['Company']['company_name'];
// 			$contents = $this->getMailTemplate("pwd_confirm", $email_data);
// 			$subject = $this->Prop->getValue('config.mail_title.pwd_confirm');
// 			$this->MailJob->saveMsg('web@localhost:82', $par, $subject, $contents, "", 'info@localhost:82');
// 			$this->render('find_pwd_success');
// 			exit();
// 		}
	
// 	}

// 	function pwd_confirm() {
// 		$data = $this->Session->read('pwd_remain_id');
// 		//		pr($data);
// 		$company_id = $data['Company']['id'];
// 		$this->data['Company']['pwd_remain_id'] = $data['SysParams']['name'];
// 		$user_answer = $this->params['data']['Company']['user_answer'];
// 		$condition = "\"Company\".\"pwd_remain_answer\"=" . "'$user_answer'" . " and \"Company\".\"id\" = " . "'$company_id'" . "and \"Company\".deleted is null";
// 		$answer = $this->Company->find($condition, null);
// 		$login_id = $this->CompanyAccount->find("\"CompanyAccount\".\"company_id\" =" . "'$company_id'");
// 		if(empty($user_answer)) {
			
// 			$this->set('message', $this->Valid->tagErrorMsg('required', 'Company/pwd_remain_answer'));
// 			$this->render('find_pwd_confirm');
// 			exit();
// 		}else {
// 			if(empty($answer)) {
// 				$this->set('message', $this->Valid->tagErrorMsg('incorrect', 'Company/pwd_remain_answer'));
// 				//		$this->data = $this->Session->read('answer_info');
// 				$this->render('find_pwd_confirm');
// 				exit();
// 			}

// 			else {
// 				$this->set('login_id', $login_id);
// 				//validate success
// 				$this->render("pwd_add");
// 				exit();
// 			}
// 		}
// 	}

	/**
	 * @author zhouguoyan
	 * @desc 找回密码 输入密码找回的答案
	 **/
	
// 	function pwd_add($confirm_id = null) {
// 		$company_id = substr($confirm_id, 32);
// 		$remain_key = substr($confirm_id, 0, 32);
// 		$remain_data = $this->Company->find('"Company"."id" = ' . $company_id . ' and "Company"."deleted" is null');
// 		$data = $this->Session->read('mail_info');
		
// 		//パスワード有効期限チェック　add@090825 by yangjie start
// 		if(md5($remain_data['Company']['remain_key']) != $remain_key) {
// 			$this->showError('password_remain_key_error_company');
// 		}
// 		if(date("Y-m-d H:i:s") > $remain_data['Company']['remained']) {
// 			$this->showError('password_remain_key_timeout_company');
// 		}
// 		//パスワード有効期限チェック　add@090825 by yangjie end	
		

// 		$pwd_remain_id = $this->SysParams->find("SysParams.kbn = 'password_question_id'" . ' and SysParams.id = ' . $remain_data['Company']['pwd_remain_id']);
// 		$pwd_remain_id['Company']['id'] = $company_id;
// 		$this->data['Company']['pwd_remain_id'] = $pwd_remain_id['SysParams']['name'];
// 		$this->Session->write('pwd_remain_id', $pwd_remain_id);
// 		$this->render('find_pwd_confirm');
	
// 	}

	/**
	 * @author zhouguoyan
	 * @desc 保存密码
	 **/
// 	function pwd_save() {
// 		$message = '';
// 		$message = $this->CompanyAccount->Pwd_Valid($this->Valid, $this->params['data']);
// 		$data = $this->Session->read('pwd_remain_id');
// 		$login_id = $this->CompanyAccount->find("\"CompanyAccount\".\"company_id\"=" . $data['Company']['id']);
// 		if($login_id['CompanyAccount']['login_id'] == $this->data['CompanyAccount']['login_pwd'] && $this->data['CompanyAccount']['login_pwd'] != null) {
// 			$message .= $this->Valid->tagErrorMsg('reg_pwd_repeat', 'CompanyAccount/login_pwd');
// 		}
// 		if(!empty($message)) {
// 			//validate failed
			

// 			$this->set('login_id', $login_id);
// 			$this->set("message", $message);
// 			$this->render('pwd_add');
// 			exit();
// 		}else {
// 			//validate success
			

// 			$company_id = $data['Company']['id'];
// 			$condition = "\"CompanyAccount\".\"deleted\" is null AND \"CompanyAccount\".\"company_id\"=" . "'$company_id'";
// 			$id = $this->CompanyAccount->find($condition);
// 			$info = array();
// 			$info['CompanyAccount']['login_pwd'] = md5($this->data['CompanyAccount']['login_pwd']);
// 			$info['CompanyAccount']['company_id'] = $data['Company']['id'];
// 			$info['CompanyAccount']['id'] = $id['CompanyAccount']['id'];
			
// 			$data['name'] = $id['CompanyAccount']['name'];
// 			$data['email'] = $id['CompanyAccount']['email'];
// 			$contents = $this->getMailTemplate("pwd_find_finish", $data);
// 			$subject = $this->Prop->getValue('config.mail_title.pwd_find');
			
// 			if($this->Session->check('pwd_remain_id')) {
// 				$this->CompanyAccount->save($info);
// 				$this->MailJob->saveMsg('web@localhost:82', $data['email'], $subject, $contents, "", 'info@localhost:82');
// 			}
// 			$this->render('pwd_finish');
			
// 			$this->Session->del('answer_info');
// 			$this->Session->del('mail_info');
// 			$this->Session->del('reg_info');
// 			exit();
// 		}
// 	}

	/**
	 * @author zhouguoyan
	 * @desc 验证登陆
	 **/
// 	function login() {
// 		$this->pageTitle = "カモメ中国転職：企業会員ログイン";
// 		$redirect = $this->Session->read('login_redirect');
// 		$message = $this->CompanyAccount->Login_Valid($this->Valid, $this->params['data']);
// 		$username = Sanitize::sql($this->params['data']['CompanyAccount']['login_id']);
// 		$pwd = md5($this->params['data']['CompanyAccount']['login_pwd']);
// 		$this->loginException($message, $message != null);
// 		//アカウントをチエックする
// 		$check_name = $this->CompanyAccount->find("\"CompanyAccount\".\"login_id\" = '" . $username . "'" . " AND \"Company\".\"company_type_id\" =2");
// 		$this->loginException($this->Valid->tagErrorMsg('name01_incorrect', 'CompanyAccount/login_id'), $check_name['CompanyAccount']['login_id'] == null && $username != null);
// 		$missTimes = $check_name['CompanyAccount']['last_miss_login_time'];
// 		$id = $check_name['CompanyAccount']['id'];
// 		$message = $this->accountIsLock($missTimes);
// 		$this->loginException($message, $message != null);
// 		//パスワードをチェックする
// 		$condition = "\"CompanyAccount\".\"login_id\" = '" . $username . "'" . " AND \"CompanyAccount\".\"login_pwd\" = '" . $pwd . "' AND \"CompanyAccount\".\"deleted\" is null";
// 		$check_pwd = $this->CompanyAccount->find($condition);
// 		if($check_pwd == null && $pwd != null && $username != null) {
// 			$this->setLastMissLoginTime($id, $missTimes);
// 			$this->loginException($this->Valid->tagErrorMsg('pwd01_incorrect', 'CompanyAccount/login_pwd'));
// 		} else {
// 			$this->missLoginTimeClear($id);
// 			$this->loginOk($check_pwd, $redirect);
// 		}
// 	}
	
	/**
	 * ログイン成功した場合
	 *
	 * create: 2012-03-03 ツバサ（エスワイシステム）
	 * update:
	 * */
// 	private function loginOk($check_pwd, $redirect) {
// 		//validate success
// 		$check_pwd['CompanyAccount']['company_name'] = $check_pwd['Company']['company_name'];
// 		$check_pwd['CompanyAccount']['company_alias'] = $check_pwd['Company']['company_alias'];
// 		$this->rdCompany->set($check_pwd['CompanyAccount']);
// 		if(isset($this->params['data']['CompanyAccount']['save']) && $this->params['data']['CompanyAccount']['save'] == 1) {
// 			$str1 = md5($this->data['CompanyAccount']['login_pwd']);
// 			$company_cookie_info = $str1 . $this->rdCompany->id;
// 			$this->set_Cookie("company_cookie", $company_cookie_info, 30);
// 		}
// 		//从新生成session_id值 added 20101231 by keynes yangqingsong
// 		$this->Session->renew();
// 		$this->Session->del('login_redirect');
// 		$this->redirect("https://" . str_replace("www.", "", $_SERVER['SERVER_NAME']) . "" . $redirect);
// 		exit();
// 	}
	
	/**
	 * MissLoginTimeをヌルにする
	 *
	 * create:2012-03-0２ ツバサ（エスワイシステム）
	 * updata:
	 * */
// 	private function missLoginTimeClear($id) {
// 		$data = array('id'=>$id, 'last_miss_login_time'=>null);
// 		$this->CompanyAccount->save($data);
// 	}
	
	/**
	 * MissLoginTimeをセットする
	 *
	 * create:2012-03-01 ツバサ（エスワイシステム）
	 * updata:
	 * */
// 	private function setLastMissLoginTime($id, $missTimes = null) {
// 		$canTry = $this->Prop->getValue('config.login_can_try');
// 		$subLength = $this->Prop->getValue('config.sub_length');
// 		$strlen = strlen($missTimes);
// 		$maxlen = ($canTry - 1) * $subLength;
// 		if($strlen > $maxlen) {
// 			$subidx = $strlen - $maxlen;
// 			$missTimes = substr($missTimes, $subidx);
// 		}
// 		$missTimes = $missTimes.time();
// 		$data = array('id'=>$id, 'last_miss_login_time'=>$missTimes);
// 		$this->CompanyAccount->save($data);
// 	}
	
	/**
	 * データベースから取得した秒数時間の文字列を１０桁毎に割り切って、
	 * 配列を作成して、アカウントのロックロック状態を判断する
	 * アカウントがロックされた場合、エラメッセージを返す
	 * そうではないとヌルを返す
	 *
	 * create:2012-03-01 ツバサ（エスワイシステム）
	 * updata:
	 * */
// 	private function accountIsLock($missTimes) {
// 		$times = array();
// 		$nowTime = time();
// 		$trySecond = $this->Prop->getValue('config.login_try_second');
// 		$canTry = $this->Prop->getValue('config.login_can_try');
// 		$lockSecond = $this->Prop->getValue('config.login_lock_second');
// 		$subLength = $this->Prop->getValue('config.sub_length');
// // 		echo $trySecond.','.$canTry.','.$lockSecond.','.$subLength;
// 		$arylen = strlen($missTimes) / $subLength;
// 		if($arylen < $canTry) {
// 			return null;
// 		}
// 		for($i = 0; $i < $arylen; $i++) {
// 			$times[$i] = substr($missTimes, $i*$subLength, $subLength);
// 		}
// 		$lastIdx = $arylen - 1;
// 		$firstIdx = $arylen - $canTry;
// 		if($times[$lastIdx] - $times[$firstIdx] < $trySecond
// 				&& $nowTime - $times[$lastIdx] < $lockSecond) {
// 			$str1 = $this->Valid->tagErrorMsg('account_is_lock1', 'CompanyAccount/login_id');
// 			$str2 = (int) ($lockSecond / 60);
// 			$str3 = $this->Valid->tagErrorMsg('account_is_lock2', 'CompanyAccount/login_id');
// 			return $str1.$str2.$str3;
// 		}
// 		return null;
// 	}
	
	/**
	 * ログイン例外処理
	 *
	 * create: 2012-03-03 ツバサ（エスワイシステム）
	 * update:
	 * */
// 	private function loginException($message, $bool = true) {
// 		if($bool) {
// 			$this->set('message', $message);
// 			$this->params['data']['CompanyAccount']['login_pwd'] = '';
// 			$this->render('index');
// 			exit();
// 		}
// 	}
}
?>